package com.epam.web_project.bank.command.show_commands;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ShowWorkingAccountsCommand implements Command {

	/*show admin a page with all working accounts displayed
	*/
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader props=AppPropertiesLoader.getInstance();
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO = daoFactory.getCustomerDAO();
		List<Customer> customers = customerDAO.findByApprovedAcc();
		if(customers!=null && !customers.isEmpty()) {
			request.setAttribute("customers", customers);
		}
		HttpSession session = request.getSession();
		request.setAttribute("message", session.getAttribute("message"));
		session.removeAttribute("message");
		return props.getProperty("working_accounts_page");
	}

}
